<template>
  <div class="address">
    <div class="container">
      <div class="address-title">地址管理</div>
      <div class="address-con">
        <div class="addAddress">
          <div class="address-info">
            <div class="address-info-title">新增收货地址</div>
            <el-form
              :label-position="labelPosition"
              label-width="80px"
              :model="addressInfo"
            >
              <el-form-item label="地区城市">
                <el-cascader
                  v-model="value"
                  :options="options"
                  @change="handleChange"
                ></el-cascader>
              </el-form-item>
              <el-form-item label="地区名称">
                <el-input v-model="addressInfo.area_name"></el-input>
              </el-form-item>
              <el-form-item label="详细地址">
                <el-input v-model="addressInfo.desc"></el-input>
              </el-form-item>
              <el-form-item label="邮编">
                <el-input v-model="addressInfo.post_code"></el-input>
              </el-form-item>
            </el-form>
            <div class="operate">
              <button
                class="primary-button"
                @click="preserveAddress(addressInfo)"
              >
                提交
              </button>
            </div>
          </div>
        </div>
        <div class="addressInfo">
          <AddressExhibition></AddressExhibition>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import AddressExhibition from "@/components/AddressExhibition.vue";
import { mapState } from "vuex";
import {
  getCity,
  referAddress,
  getDefault,
  getAddress,
} from "../assets/request.js";
export default {
  data() {
    return {
      value: [],
      options: [],
      addressInfo: {
        name: "",
        phone: "",
        password: "",
        area: "",
        area_name: "",
        desc: "",
        post_code: "",
      },
      defaultAddress: {},
      labelPosition: "right",
      addressArray: [],
    };
  },
  methods: {
    async preserveAddress(addressInfo) {
      const resReferAddress = await referAddress(addressInfo);
      this.getAddressArray();
      this.clearInfo(addressInfo);
    },
    clearInfo(addressInfo) {
      for (let item in addressInfo) {
        if (!["name", "phone", "password"].includes(item)) {
          addressInfo[item] = "";
        }
      }
    },
    handleChange(value) {
      this.addressInfo.area = value.join(",");
    },
    async getAddressArray() {
      const resGetAddress = await getAddress();
      this.addressArray = resGetAddress.data.result;
    },
  },
  computed: {
    ...mapState("userOption", ["user", "username"]),
  },
  components: {
    AddressExhibition,
  },
  async mounted() {
    const resCity = await getCity();
    this.options = resCity.data.result;
    this.addressInfo.name = this.username;
    this.addressInfo.phone = this.user.phone;
    this.addressInfo.password = this.user.password;
    this.getAddressArray();
  },
};
</script>

<style lang="scss" scoped>
@import "../assets/style/coverBtn.scss";
.address {
  width: 100%;
  .container {
    width: 1200px;
    margin: 0 auto;
    .address-title {
      display: flex;
      justify-content: start;
      align-items: center;
      width: 100%;
      height: 50px;
      font-size: 20px;
    }
    .address-con {
      display: flex;
      justify-content: space-between;
      width: 1000px;
      margin: 0 auto;
      .address-info-title {
        height: 40px;
        padding-left: 20px;
        margin-bottom: 30px;
        background-color: #2f333a;
        line-height: 40px;
        color: white;
        font-size: 16px;
      }
      .address-info {
        width: 300px;
        padding: 30px;
        .operate {
          display: flex;
          justify-content: center;
          align-items: center;
          width: 100%;
          height: 50px;
          button {
            width: 100px;
            height: 40px;
            font-size: 12px;
          }
        }
      }
      .addressInfo {
      }
    }
  }
}
</style>
